package com.xxg.renrenproduct.service;

import com.alibaba.fastjson.JSON;
import com.xxg.renrencommon.api.SearchManager;
import com.xxg.renrencommon.domain.bean.Product;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrenproduct.dao.ProductCollectionInfoDao;
import com.xxg.renrenproduct.dao.RenrenShopGoodsDao;
import com.xxg.renrencommon.domain.bean.ProductCollectionInfo;
import com.xxg.renrenproduct.domain.query.AddProductCollectionQuery;
import com.xxg.renrenproduct.domain.query.GetCollectionListQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类说明
 *
 * @author szj
 * @date 2021/11/30
 */
@Service
@Slf4j
public class ProductCollectionServiceImpl implements ProductCollectionService{
    @Resource
    private ProductCollectionInfoDao productCollectionInfoDao;
    @Resource
    private RenrenShopGoodsDao renrenShopGoodsDao;
    @DubboReference(version = "1.0.0")
    private SearchManager searchManager;

    @Override
    public BaseResult<Void> add(String userId, AddProductCollectionQuery query) {
        int userIdInt = Integer.parseInt(userId);
        ProductCollectionInfo productCollectionInfo = productCollectionInfoDao.selectByUserIdAndSpuIdAndSkuId(userIdInt, query.getSpuId(), query.getSkuId());
        if (productCollectionInfo == null) {
            Integer originShopId = renrenShopGoodsDao.selectShopIdByPrimaryKey(query.getSpuId());
            productCollectionInfo = new ProductCollectionInfo();
            productCollectionInfo.setSpuId(query.getSpuId());
            productCollectionInfo.setSkuId(query.getSkuId());
            productCollectionInfo.setOriginShopId(originShopId);
            productCollectionInfo.setUserId(userIdInt);
            productCollectionInfoDao.insert(productCollectionInfo);
        }
        return new BaseResult<>();
    }

    @Override
    public BaseResult<Void> cancel(String userId, AddProductCollectionQuery query) {
        productCollectionInfoDao.deleteByUserIdAndSpuIdAndSkuId(Integer.parseInt(userId), query.getSpuId(), query.getSkuId());
        return new BaseResult<>();
    }

    @Override
    public BaseResult<List<Product>> list(String userId, GetCollectionListQuery query) {
        List<ProductCollectionInfo> productCollectionInfos = productCollectionInfoDao.findByUserId(Integer.parseInt(userId), query.getPageNo(), query.getPageSize());
        List<String> collect = productCollectionInfos.stream()
                .map(productCollectionInfo -> productCollectionInfo.getSpuId() + "_" + productCollectionInfo.getSkuId()).collect(Collectors.toList());
        String skuInfo = searchManager.getSkuInfo(collect);
        List<Product> products = JSON.parseArray(skuInfo, Product.class);
        return new BaseResult<>(products);
    }

    @Override
    public BaseResult<List<String>> listBySpuId(String userId, Integer spuId) {
        List<ProductCollectionInfo> productCollectionInfos = productCollectionInfoDao.findByUserIdAndSpuId(Integer.parseInt(userId), spuId);
        if (productCollectionInfos == null) {
            return new BaseResult<>();
        }
        List<String> collect = productCollectionInfos.stream().map(productCollectionInfo -> productCollectionInfo.getSpuId() + "_" + productCollectionInfo.getSkuId()).collect(Collectors.toList());
        return new BaseResult<>(collect);
    }
}
